package org.jeecg.modules.zcgl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgBaseController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.zcgl.entity.PublicAccountTransactionInquiryDetails;
import org.jeecg.modules.zcgl.service.IPublicAccountTransactionInquiryDetailsService;
import org.jeecg.modules.zcgl.service.IPublicAccountTransactionInquiryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 对公账户交易明细记录详情
 * @Author: jeecg-boot
 * @Date: 2021-02-01
 * @Version: V1.0
 */
@Api(tags = "对公账户交易明细记录详情")
@RestController
@RequestMapping("/zcgl/publicAccountTransactionInquiryDetails")
@Slf4j
public class PublicAccountTransactionInquiryDetailsController extends JeecgBaseController<PublicAccountTransactionInquiryDetails, PublicAccountTransactionInquiryDetails, IPublicAccountTransactionInquiryDetailsService> {
    @Autowired
    private IPublicAccountTransactionInquiryDetailsService publicAccountTransactionInquiryDetailsService;
    @Autowired
    private ISysDepartService departService;
    @Autowired
    private IPublicAccountTransactionInquiryService inquiryService;

    /**
     * 分页列表查询
     *
     * @param publicAccountTransactionInquiryDetails
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "对公账户交易明细记录详情-分页列表查询")
    @ApiOperation(value = "对公账户交易明细记录详情-分页列表查询", notes = "对公账户交易明细记录详情-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(PublicAccountTransactionInquiryDetails publicAccountTransactionInquiryDetails,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<PublicAccountTransactionInquiryDetails> queryWrapper = QueryGenerator.initQueryWrapper(publicAccountTransactionInquiryDetails, req.getParameterMap());
        Page<PublicAccountTransactionInquiryDetails> page = new Page<PublicAccountTransactionInquiryDetails>(pageNo, pageSize);
        IPage<PublicAccountTransactionInquiryDetails> pageList = publicAccountTransactionInquiryDetailsService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param publicAccountTransactionInquiryDetails
     * @return
     */
    @AutoLog(value = "对公账户交易明细记录详情-添加")
    @ApiOperation(value = "对公账户交易明细记录详情-添加", notes = "对公账户交易明细记录详情-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody PublicAccountTransactionInquiryDetails publicAccountTransactionInquiryDetails) {
        publicAccountTransactionInquiryDetailsService.save(publicAccountTransactionInquiryDetails);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param publicAccountTransactionInquiryDetails
     * @return
     */
    @AutoLog(value = "对公账户交易明细记录详情-编辑")
    @ApiOperation(value = "对公账户交易明细记录详情-编辑", notes = "对公账户交易明细记录详情-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody PublicAccountTransactionInquiryDetails publicAccountTransactionInquiryDetails) {
        publicAccountTransactionInquiryDetailsService.updateById(publicAccountTransactionInquiryDetails);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "对公账户交易明细记录详情-通过id删除")
    @ApiOperation(value = "对公账户交易明细记录详情-通过id删除", notes = "对公账户交易明细记录详情-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        publicAccountTransactionInquiryDetailsService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "对公账户交易明细记录详情-批量删除")
    @ApiOperation(value = "对公账户交易明细记录详情-批量删除", notes = "对公账户交易明细记录详情-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.publicAccountTransactionInquiryDetailsService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "对公账户交易明细记录详情-通过id查询")
    @ApiOperation(value = "对公账户交易明细记录详情-通过id查询", notes = "对公账户交易明细记录详情-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        PublicAccountTransactionInquiryDetails publicAccountTransactionInquiryDetails = publicAccountTransactionInquiryDetailsService.getById(id);
        if (publicAccountTransactionInquiryDetails == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(publicAccountTransactionInquiryDetails);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param publicAccountTransactionInquiryDetails
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, PublicAccountTransactionInquiryDetails publicAccountTransactionInquiryDetails) {
        return super.exportXls(request, publicAccountTransactionInquiryDetails, PublicAccountTransactionInquiryDetails.class, "对公账户交易明细记录详情");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, PublicAccountTransactionInquiryDetails.class);
    }

    /**
     * 手动下载对公账户交易明细记录
     *
     * @param inquiryDetails
     * @return
     */
    @AutoLog(value = "对公账户交易明细记录-手动下载对公账户交易明细记录")
    @ApiOperation(value = "对公账户交易明细记录-下载", notes = "对公账户交易明细记录-手动下载对公账户交易明细记录")
    @PostMapping(value = "/downloadCcqtgb")
    public Result<?> downloadCcqtgb(@RequestBody PublicAccountTransactionInquiryDetails inquiryDetails) {
        if (inquiryDetails.getDate() == null) {
            return Result.errorValidation("请选择需要下载账单的日期！");
        }
        if (DateUtils.sameDate(inquiryDetails.getDate(), DateUtils.getDate()) >= 0) {
            return Result.errorValidation("需要下载账单的日期必须小于当前日期！");
        }
        List<SysDepart> list = new ArrayList<>();
        LoginUser loginUser = JwtUtil.getLoginUser();
        if (StringUtils.isNotBlank(inquiryDetails.getCompanyId())) {
            list.add(departService.getById(inquiryDetails.getCompanyId()));
        } else if (loginUser.getCurrentCompanyId().equalsIgnoreCase(jeecgPropertyConfig.getBaseDepartId())) {
            list.addAll(departService.queryCompanyList());
        } else {
            list.add(departService.getById(loginUser.getCurrentCompanyId()));
        }
        Thread t = new Thread(() -> {
            for (SysDepart depart : list) {
                try {
                    inquiryService.publicAccountTransactionInquiryByDepart(depart, inquiryDetails.getDate());
                } catch (JeecgBootException e) {
                    log.error(depart.getDepartName() + "对公账户交易明细记录下载失败：【" + e.getMessage() + "】");
                } catch (Exception e) {
                    log.error(depart.getDepartName() + "对公账户交易明细记录下载失败！");
                }
            }
        });
        t.start();

//		 //线程的线程池
//		 ExecutorService executor = Executors.newFixedThreadPool(1);
//		 CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//			 List<String> errorMessage = new ArrayList<>();
//			 for (SysDepart depart : list) {
//				 try {
//					 inquiryService.publicAccountTransactionInquiryByDepart(depart, inquiryDetails.getDate());
//				 } catch (JeecgBootException e) {
//					 errorMessage.add(depart.getDepartName() + "对公账户交易明细记录下载失败：【" + e.getMessage() + "】");
//				 } catch (Exception e){
//					 errorMessage.add(depart.getDepartName() + "对公账户交易明细记录下载失败！");
//				 }
//			 }
//			 return StringUtils.join(errorMessage, CommonConstant.DEFAULT_SEPARATOR);
//		 }, executor);
//		 //采用lambada的实现方式
//		 future.thenAccept(e -> System.out.println(e ));

        return Result.ok("对公账户交易明细记录下载中！");
    }

    @Override
    public List<PublicAccountTransactionInquiryDetails> exportList(List<PublicAccountTransactionInquiryDetails> list) {
        return list;
    }

    @Override
    public boolean importSave(PublicAccountTransactionInquiryDetails inquiryDetails) {
        return service.save(inquiryDetails);
    }
}
